<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-table :data="roleList" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="用户ID" prop="userId"></el-table-column>
            <el-table-column label="用户名称" prop="userName"></el-table-column>
            <el-table-column label="用户权限名称" prop="permission"></el-table-column>
            <el-table-column label="操作" width="150px">
              <template v-slot="scope">
                <el-button type="warning" icon="el-icon-setting" size="mini"
                  v-on:click="showRolesDialog(scope.row.userId,scope.row.permission)">分配权限</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog title="选择用户权限" :visible.sync="rolesDialogVisible" width="33%" @close="rolesDialogClosed">
      <el-form :model="rolesForm" ref="rolesFormRef" label-width="75px">
        <el-form-item label="用户权限:">
          <el-select v-model="rolesForm.permission">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="用户" value="user"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rolesDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="rolesUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    data() {
      return {
        roleList: [],
        rolesDialogVisible: false,
        rolesForm:{
          id:'',
          permission:''
        },
      }
    },
    created() {
      this.getRoleList()
    },
    methods: {
      async getRoleList() {
        const {
          data: res
        } = await this.$http.get("https://localhost:44389/api/UserRoles")
        if (res.status !== 200) {
          this.$message.error("获取用户失败")
        }
        this.roleList = res.data
      },
      showRolesDialog(id, permiss) {
        this.rolesDialogVisible = true
        this.rolesForm.permission = permiss
        this.rolesForm.id = id
      },
      rolesDialogClosed() {
        this.$refs.rolesFormRef.resetFields()
      },
       rolesUserInfo() {
        axios({
          method:'put',
          url:'https://localhost:44389/api/UserRoles/'+this.rolesForm.id,
          data:this.rolesForm,
        }).then(res=>{
          if(res.status!==200){return this.$message.error("修改失败")}
          this.rolesDialogVisible =false
          this.getRoleList()
          this.$message.success('修改成功')
        })
      }
    },
  }
</script>

<style lang="less" scoped>
</style>
